package com.zy.mc_module.wheel.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.LeadingMarginSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zy.mc_module.R;
import com.zy.mc_module.utils.AppTools;
import com.zy.mc_module.utils.GlideUtil;
import com.zy.mc_module.utils.SizeUtils;
import com.zy.mc_module.utils.StringsUtils;
import com.zy.mc_module.view.CustomTextView;
import com.zy.mc_module.wheel.recyclerview.divider.ItemDivider;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * Created on 18/11/22
 * 通用Dialog
 */
public class CommonDialog extends Dialog {
    private View view;
    protected int layoutId;
    private Context context;
    private List<String> mLists;
    private OnItemClickListener itemClickListener;//通用布局item的点击事件
    private boolean isBottom = true;

    /***
     * 自定义布局
     * @param context
     * @param view
     */
    public CommonDialog(Context context, int themeResId, View view) {
        super(context, themeResId);
        this.view = view;
        this.layoutId = 0;
        this.context = context;
    }

    public CommonDialog(Context context, boolean isBottom, View view) {
        super(context, R.style.BaseDialog);
        this.context = context;
        this.isBottom = isBottom;
        this.view = view;
    }

    public CommonDialog(Context context, int themeResId, int layoutId) {
        super(context, themeResId);
        this.context = context;
        this.layoutId = layoutId;
        this.view = LayoutInflater.from(context).inflate(layoutId, null, false);
    }

    public CommonDialog(Context context, int layoutId) {
        super(context, R.style.BottomDialog);
        this.context = context;
        this.layoutId = layoutId;
        this.view = LayoutInflater.from(context).inflate(layoutId, null, false);
    }

    public CommonDialog(Context context, View view) {
        super(context, R.style.BottomDialog);
        this.view = view;
        this.layoutId = 0;
        this.context = context;
    }

    /***
     * 通用布局--RecyclerView+Textview
     * @param context
     */
    public CommonDialog(Context context, List<String> mLists, OnItemClickListener itemClickListener) {
        super(context, R.style.BottomDialog);
        this.mLists = mLists;
        this.context = context;
        this.layoutId = R.layout.dialog_common;
        this.itemClickListener = itemClickListener;
        this.view = LayoutInflater.from(context).inflate(layoutId, null, false);
    }

    public CommonDialog(Context context, String[] strings, OnItemClickListener itemClickListener) {
        super(context, R.style.BottomDialog);
        this.mLists = Arrays.asList(strings);
        this.context = context;
        this.layoutId = R.layout.dialog_common;
        this.itemClickListener = itemClickListener;
        this.view = LayoutInflater.from(context).inflate(layoutId, null, false);
    }

    public CommonDialog(Context context, int layoutId, String[] strings, OnItemClickListener itemClickListener) {
        super(context, R.style.BottomDialog);
        this.mLists = Arrays.asList(strings);
        this.context = context;
        this.layoutId = layoutId;
        this.itemClickListener = itemClickListener;
        this.view = LayoutInflater.from(context).inflate(layoutId, null, false);
    }

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        view.setMinimumWidth(10000);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(view);
        if (isBottom) {
            Objects.requireNonNull(getWindow()).setGravity(Gravity.BOTTOM); //显示在底部
            getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        //初始化视图
        initView(view);
    }

    protected void initView(View view) {
        if (layoutId == R.layout.dialog_common) {
            //通用 垂直RecyclerView弹窗
            TextView tv_dis = view.findViewById(R.id.reason_dis);
            RecyclerView recyclerView = view.findViewById(R.id.reason_recycler);
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
            recyclerView.addItemDecoration(new ItemDivider());
            if (null == mLists) {
                mLists = new ArrayList<>();
            }
            //设置RecyclerView高度
            ViewGroup.LayoutParams lp = recyclerView.getLayoutParams();
            if (mLists.size() > 6) {
                lp.height = SizeUtils.dp2px(context, 49 * 6);
            } else {
                lp.height = SizeUtils.dp2px(context, 49 * mLists.size());
            }
            recyclerView.setLayoutParams(lp);
            CommonDialogAdapter adapter = new CommonDialogAdapter(context, mLists, 0);
            recyclerView.setAdapter(adapter);
            adapter.setItemClickListener(position -> {
                if (null != itemClickListener) {
                    itemClickListener.onItemClick(position);
                    dismiss();
                }
            });
            tv_dis.setOnClickListener(v -> dismiss());
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public void showScale() {
        super.show();
        ScaleAnimation scaleAnimation = new ScaleAnimation(0f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(500);
        this.view.startAnimation(scaleAnimation);
    }

    public void dismissScale() {
        ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 0f, 1f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(240);
        scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                dismiss();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        this.view.startAnimation(scaleAnimation);
    }

    @Override
    public void dismiss() {
        if (null != this && this.isShowing()) {
            super.dismiss();
        }
    }

    /**
     * 首行缩进的SpannableString
     *
     * @param description 描述信息
     */
    private SpannableString getSpannableString(String description) {
        SpannableString spannableString = new SpannableString(description);
        int marginSpanSize = SizeUtils.dp2px(context, 30 + 2);//文字宽度+ 背景padding4dp+间隔2dp
        LeadingMarginSpan leadingMarginSpan = new LeadingMarginSpan.Standard(marginSpanSize, 0);//仅首行缩进
        spannableString.setSpan(leadingMarginSpan, 0, description.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }
}
